package com.ruoyi.supermarket.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.supermarket.domain.Stock;
import com.ruoyi.supermarket.mapper.StockMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.supermarket.mapper.WarehouseMapper;
import com.ruoyi.supermarket.domain.Warehouse;
import com.ruoyi.supermarket.service.IWarehouseService;

/**
 * 货位Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-05-19
 */
@Service
public class WarehouseServiceImpl implements IWarehouseService 
{
    @Autowired
    private WarehouseMapper warehouseMapper;
    @Autowired
    private StockMapper stockMapper;

    /**
     * 查询货位
     * 
     * @param whId 货位主键
     * @return 货位
     */
    @Override
    public AjaxResult selectWarehouseByWhId(Long whId)
    {
        Stock stock = new Stock();
        stock.setWhId(whId);
        List<Stock> list = stockMapper.selectStockList(stock);
        if(list!=null && list.size()>0){
            return AjaxResult.error(99,"该货位已有库存，不能修改");
        }else{
            return AjaxResult.success(warehouseMapper.selectWarehouseByWhId(whId));
        }
    }

    /**
     * 查询货位列表
     * 
     * @param warehouse 货位
     * @return 货位
     */
    @Override
    public List<Warehouse> selectWarehouseList(Warehouse warehouse)
    {
        return warehouseMapper.selectWarehouseList(warehouse);
    }

    /**
     * 新增货位
     * 
     * @param warehouse 货位
     * @return 结果
     */
    @Override
    public AjaxResult insertWarehouse(Warehouse warehouse)
    {
        warehouseMapper.insertWarehouse(warehouse);
        return AjaxResult.success();
    }

    /**
     * 修改货位
     * 
     * @param warehouse 货位
     * @return 结果
     */
    @Override
    public int updateWarehouse(Warehouse warehouse)
    {
        return warehouseMapper.updateWarehouse(warehouse);
    }

    /**
     * 批量删除货位
     * 
     * @param whIds 需要删除的货位主键
     * @return 结果
     */
    @Override
    public int deleteWarehouseByWhIds(Long[] whIds)
    {
        return warehouseMapper.deleteWarehouseByWhIds(whIds);
    }

    /**
     * 删除货位信息
     * 
     * @param whId 货位主键
     * @return 结果
     */
    @Override
    public AjaxResult deleteWarehouseByWhId(Long whId)
    {
        Stock stock = new Stock();
        stock.setWhId(whId);
        List<Stock> list = stockMapper.selectStockList(stock);
        if(list!=null && list.size()>0){
            return AjaxResult.error(99,"该货位已有库存，不能删除");
        }else{
            return AjaxResult.success(warehouseMapper.deleteWarehouseByWhId(whId));
        }
    }

    @Override
    public boolean findWarehouseName(Warehouse warehouse) {
        Long whId = StringUtils.isNull(warehouse.getWhId()) ? -1L : warehouse.getWhId();
        Warehouse w = warehouseMapper.selectWarehouseByWhName(warehouse.getWhName());
        if (w != null && w.getWhId().longValue() != whId.longValue()){
            return false;
        }
        return true;
    }
}
